package com.wxapp.pay.service.pay.integral;

import com.wxapp.common.constant.enums.SysNameEnum;
import com.wxapp.common.constant.pojo.vo.FeignRequestVo;
import com.wxapp.common.core.filter.except.ThrowMyException;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.feign.DynamicFeignUtil;
import com.wxapp.common.util.http.HttpRequestUtil;
import com.wxapp.common.util.random.RandomUtil;
import com.wxapp.pay.api.pojo.pojo.po.vo.custom.PCustomPayVo;
import com.wxapp.pay.api.pojo.pojo.vm.pay.PPayVm;
import com.wxapp.pay.api.pojo.pojo.vo.order.QOrderInfoVo;
import com.wxapp.pay.api.pojo.pojo.vo.payLogs.QPayLogsAddVo;
import com.wxapp.pay.service.event.PayLogsEvent;
import com.wxapp.pay.service.service.PayConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author lgl
 */
@Component
@RequiredArgsConstructor
public class IntegralPay extends  BaseIntegralPay{
    private final static String payWay="IntegralPay";
    private final ApplicationEventPublisher publisher;
    private final DynamicFeignUtil dynamicFeignUtil;
    private final PayConfigService payConfigService;
    @Override
    protected String getType() {
        return payWay;
    }

    @Override
    protected PPayVm execute(QOrderInfoVo vo){
        PCustomPayVo pCustomPayVo = new PCustomPayVo();
        pCustomPayVo.setBalanceIntegral(vo.getQOrderVm().getPrice());
        pCustomPayVo.setBalanceRmb(new BigDecimal(0));
        pCustomPayVo.setUserId(vo.getQOrderVm().getCreateBy());
        if (pay(pCustomPayVo)){
            return new PPayVm(200,"成功", RandomUtil.getCommonRandom(6),null);
        }
        return new PPayVm(400,"失败",null,null);
    }

    @Override
    protected void publicEvent(QPayLogsAddVo vo, ApplicationEvent... event) {
        publisher.publishEvent(new PayLogsEvent(this,"",vo));
        if (CheckUtil.isNullArray(event)) {
            return;
        }
        for (ApplicationEvent e : event) {
            publisher.publishEvent(e);
        }
    }

    @Override
    protected Boolean checkResponse(PPayVm<String> vm, QOrderInfoVo vo) {
        return vm.getCode() == 200;
    }

    @Override
    protected Boolean redirect(QOrderInfoVo vo) {
        try {
            switch (vo.getRequestType()) {
                case HTTP_REQUEST -> HttpRequestUtil.request(vo.getHttpRedirect());
                case FEIGN_REQUEST -> dynamicFeignUtil.executeApi(vo.getFeignRedirect());
            }
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从数据库中获取支付配置
     */
    private Map<String, String> getConfigs() throws ThrowMyException {
        var config = payConfigService.getConfigsByCode(payWay);
        if (CheckUtil.isNullMap(config.getData())) {
            throw new ThrowMyException("没有找到支付方式");
        }
        return config.getData();
    }

    private boolean pay(PCustomPayVo vo){
        FeignRequestVo<PCustomPayVo> voRequestVo = new FeignRequestVo<>(SysNameEnum.CUSTOM,"custom/account/pay", HttpMethod.POST);
        voRequestVo.setParams(vo);
         var objectResultVm = dynamicFeignUtil.executeApi(voRequestVo);
        return objectResultVm.getCode() == 200;
    }
}
